/*
 * @lc app=leetcode.cn id=160 lang=javascript
 *
 * [160] 相交链表
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */

/**
 * @param {ListNode} headA
 * @param {ListNode} headB
 * @return {ListNode}
 */
var getIntersectionNode = function(headA, headB) {
  let map = new Map();
  let p1 = headA;
  while(p1) {
    map.set(p1, 1);
    p1 = p1.next;
  }
  let p2 = headB;
  while(p2) {
    if (map.has(p2)) {
      return p2;
    }
    p2 = p2.next;
  }

  return null;
};
// @lc code=end

